package com.iraeta.service.impl;

import com.iraeta.dao.CaizhiDao;
import com.iraeta.entity.Caizhi;
import com.iraeta.service.CaiZhiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CaiZhiServiceImpl implements CaiZhiService {
    @Autowired
    CaizhiDao caizhiDao;

    @Transactional
    @Override
    public void addCaiZhi(Caizhi caizhi) {
        caizhi.setCdefine2("0");
        caizhiDao.insert(caizhi);
        int pId = caizhi.getId();
        caizhi.setId(0);
        caizhi.setCdefine2(String.valueOf(pId));
        caizhiDao.insert(caizhi);
    }

    @Transactional
    @Override
    public void updateCaiZhi(Caizhi caizhi) {
        caizhi.setCdefine2("0");
        caizhiDao.updateByPrimaryKey(caizhi);
        int pId = caizhi.getId();
        caizhi.setId(0);
        caizhi.setCdefine2(String.valueOf(pId));
        caizhiDao.insert(caizhi);
    }

    @Override
    public void deleteCaiZhi(Integer id) {
        caizhiDao.deleteByPrimaryKey(id);
    }

    @Override
    public Caizhi getCaiZhi(Integer id) {
        return caizhiDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Caizhi> listAllCaiZhi() {
        return caizhiDao.selectAll();
    }

    @Override
    public List<Caizhi> listCaiZhi(String name) {
        return caizhiDao.selectByName(name);
    }

    @Override
    public List<Caizhi> listHisCaiZhi(Integer id) {
        return caizhiDao.selectByCdefine2(String.valueOf(id));
    }
}
